<?php
namespace App\Http\Controllers\Admin;
use App\http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class OrderController extends BaseController{
    //订单列表页面
    public function order_list(){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        return view('admin/order/order_list');
    }
    //获取订单列表数据
    public function toget_order_list(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $order_p = DB::table("order as o")
        ->leftJoin("user as u","u.user_id","o.user_id")
        ->orderBy("o.order_addtime","DESC");
        if($request->consignee){
            $order_p = $order_p->where("consignee","like","%".$request->consignee."%");
        }
        if($request->order_number){
            $order_p = $order_p->where("order_number",$request->order_number);
        }
        if($request->is_pay){
            $order_p = $order_p->where("is_pay",$request->is_pay);
        }
        if($request->is_sign){
            $order_p = $order_p->where("is_sign",$request->is_sign);
        }
        if($request->start){
            $order_p = $order_p->where("order_addtime",">=",strtotime($request->start));
        }
        if($request->end){
            $order_p = $order_p->where("order_addtime","<=",strtotime($request->end));
        }
        $order_p = $order_p->paginate(5);
        // ->paginate(5);
        // dd($order);
        // echo $order_p->render();
        // dd($request->op);
        // die();
		$order = $order_p->items();
        $current = $order_p->currentPage();
        $a = ($current-1)*5+1;
        echo 
        '
        <div class="layui-form" >
            <table class="layui-table">
                <thead>
                    <tr>
                        <th>
                        <input type="checkbox" name="all" value="" lay-skin="primary" lay-filter="all" />
                        </th>
                        <th>订单编号</th>
                        <th>收货人</th>
                        <th>性别</th>
                        <th>收货地址</th>
                        <th>联系方式</th>
                        <th>支付金额</th>
                        <th>支付状态</th>
                        <th>签收状态 </th>
                        <th>发货状态</th>
                        <th>签收时间</th>
                        <th>发货时间</th>
                        <th >操作</th>
                    </tr>
                </thead>
                <tbody>
        ';
        foreach ($order as $key => $value) {
            // $order_addtime =strtotime($value->order_addtime);
            $sex = $value->sex==1?"男":"女";
            $is_pay = $value->is_pay==1?"未支付":"已支付";
            $is_sign = $value->is_sign==1?"未签收":"已签收";
            $is_consign = $value->is_consign==1?"未发货":"已发货";
            if($value->sign_time){
                $value->sign_time =date("Y-m-d",$value->sign_time);
            }else{
                $value->sign_time = "----";
            }
            if($value->consign_time){
                $value->consign_time = date("Y-m-d",$value->consign_time);
            }else{
                $value->consign_time = "----";
            }
            echo 
            '
            <tr>
                <td><input type="checkbox" name="id[]" value="'.$value->order_id.'" lay-skin="primary" lay-filter="item" /></td>
                <td>'.$value->order_number.'</td>
                <td>'.$value->consignee.'</td>
                <td>'.$sex.'</td>
                <td>'.$value->address.'</td>
                <td>'.$value->telephone.'</td>
                <td>'.$value->pay_price.'</td>
                <td>'.$is_pay.'</td>
                <td>'.$is_sign.'</td>
                <td>'.$is_consign.'</td>
                <td>'.$value->sign_time.'</td>
                <td>'.$value->consign_time.'</td>
                <td class="td-manage">
                <a title="查看"  onclick="x_admin_show('."'编辑'".','."'/admin/edit_order?oid=$value->order_id'".')" href="javascript:;">
                    <i class="layui-icon">&#xe63c;</i>
                </a>
                <a title="删除" onclick="member_del(this,'.$value->order_id.')" href="javascript:;">
                    <i class="layui-icon">&#xe640;</i>
                </a>
                </td>
            </tr>
            ';
            $more = DB::table("order_goods as o")
                    ->leftJoin("good as g","g.good_id","o.good_id")
                    ->where("order_id",$value->order_id)
                    ->get();
            echo
            '
            <tr>
                <td colspan="14" class="more">
                    <div class="layui-collapse" style="margin-left: 45px;">
                        <div class="layui-colla-item">
                            <h2 class="layui-colla-title">更多信息</h2>
                            <div class="layui-colla-content ">
                                <button class="layui-btn" onclick="x_admin_show('."'添加订单商品'".','."'/admin/add_order_good?order_id=$value->order_id'".')"><i class="layui-icon"></i>添加</button>
                                <table class="layui-table">
                                    <thead>
                                        <tr>
                                            <th>商品名称</th>
                                            <th>商品数量</th>
                                            <th>商品单价</th>
                                            <th>商品总价</th>
                                            <th >操作</th>
                                        </tr>
                                    </thead>
                                    <tbody>
            ';
            foreach ($more as $val) {
                $sum = $val->number*$val->good_price;
                echo
                '
                <tr>
                    <td>'.$val->good_name.'</td>
                    <td ><span oid='.$val->id.' style="cursor:pointer;margin-right:8px;" class="decre">-</span><span class="ali">'.$val->number.'</span><span oid='.$val->id.' style="cursor:pointer;margin-left:8px" class="incre">+</span></td>
                    <td class="leagle">'.$val->good_price.'</td>
                    <td class="sum">'.$sum.'</td>
                    <td ><a title="删除" onclick="good_del(this,'.$val->id.')" href="javascript:;">
                            <i class="layui-icon">&#xe640;</i>
                        </a>
                    </td>
                </tr>
                ';
            }
            echo
            '
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </td>
                </tr>';
                
        }
        echo '
        </tbody>
            </table>
        </div>
        <div class="page">'.$order_p->render().'</div>
        ';
    }
    //获取订单总数
    public function toget_order_count(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $order = DB::table('order');
        if($request->consignee){
            $order = $order->where("consignee","like","%".$request->consignee."%");
        }
        if($request->order_number){
            $order = $order->where("order_number",$request->order_number);
        }
        if($request->is_pay){
            $order = $order->where("is_pay",$request->is_pay);
        }
        if($request->is_sign){
            $order = $order->where("is_sign",$request->is_sign);
        }
        if($request->start){
            $order = $order->where("order_addtime",">=",strtotime($request->start));
        }
        if($request->end){
            $order = $order->where("order_addtime","<=",strtotime($request->end));
        }
        $order = $order->count();
        // ->count();
        echo $order;
    }
    //订单添加页面
    public function add_order(){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        // dd($cat);die;
        return view("admin/order/order_add");
    }
    //订单商品添加页面
    public function add_good_order(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $order_id = $request->order_id;
        return view("admin/order/order_good_add",["order_id"=>$order_id]);
    }
    //用户搜索异步
    public function select_user(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        if($request->username){
            $user = DB::table("user")->where("user_name","like","%".$request->username."%")->get();
            foreach ($user as $value) {
                echo'
                <li class="layui-input" uid="'.$value->user_id.'" style="line-height: 35px;">'.$value->user_name.'</li>
                ';
            }
            die;
        }
    }
     //订单商品搜索异步
     public function select_good(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        if($request->good_name){
            $good = DB::table("good")->where("good_name","like","%".$request->good_name."%")->get();
            foreach ($good as $value) {
                echo'
                <li class="layui-input" gid="'.$value->good_id.'" style="line-height: 35px;">'.$value->good_name.'</li>
                ';
            }
            die;
        }
    }
    //订单添加异步
    public function todo_addorder(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $order_addtime = $request->order_addtime?strtotime($request->order_addtime):time();
        $data = array(
            "order_number"=>$request->order_number,
            "address"=>$request->address,
            "consignee"=>$request->consignee,
            "is_pay"=>$request->is_pay,
            "is_sign"=>$request->is_sign,
            "is_consign"=>$request->is_consign,
            "pay_price"=>$request->pay_price,
            "sex"=>$request->sex,
            "telephone"=>$request->telephone,
            "user_id"=>$request->user_id,
            "order_addtime"=>$order_addtime
        );
        if ($request->consign_time) {
            $data["consign_time"] = strtotime($request->consign_time);
        }
        if ($request->sign_time) {
            $data["sign_time"] = strtotime($request->sign_time);
        }
        $res = DB::table("order")->insert($data);
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"添加成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }
    }
    //订单商品添加异步
    public function todo_addordergood(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $good = DB::table("good")->where("good_id",$request->good_id)->first();
        $good_price = $good->good_price;
        $data = array(
            "order_id"=>$request->order_id,
            "good_id"=>$request->good_id,
            "number"=>$request->number,
            "good_price"=>$good_price,
            "addtime"=>time(),
        );
        $res = DB::table("order_goods")->insert($data);
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"添加成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }
    }

    //订单编辑页面
    public function edit_order(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $order_id = $request->oid;
        $order = DB::table("order")->where("order_id",$order_id)->first();
        $user = DB::table("user")->where("user_id",$order->user_id)->first();
        $order->user_name = $user->user_name;
        $order->order_addtime = date("Y-m-d",$order->order_addtime);
        $order->consign_time = date("Y-m-d",$order->consign_time);
        $order->sign_time = date("Y-m-d",$order->sign_time);
        // dd($cat);die;
        return view("admin/order/order_edit",[
                        "order"=>$order
                    ]);
    }

    //订单编辑异步
    public function todo_editorder(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $order_id = $request->order_id;
        $order_addtime = $request->order_addtime?strtotime($request->order_addtime):time();
        $data = array(
            "order_number"=>$request->order_number,
            "address"=>$request->address,
            "consignee"=>$request->consignee,
            "is_pay"=>$request->is_pay,
            "is_sign"=>$request->is_sign,
            "is_consign"=>$request->is_consign,
            "pay_price"=>$request->pay_price,
            "sex"=>$request->sex,
            "telephone"=>$request->telephone,
            "user_id"=>$request->user_id,
            "order_addtime"=>$order_addtime,
            "order_rand"=>mt_rand(1,99999)
        );
        if ($request->consign_time) {
            $data["consign_time"] = strtotime($request->consign_time);
        }
        if ($request->sign_time) {
            $data["sign_time"] = strtotime($request->sign_time);
        }
        $res = DB::table("order")->where("order_id",$order_id)->update($data);
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"保存成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }
        
    }
    //订单商品数量编辑
    public function todo_editordergood(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $num = $request->num;
        $id = $request->oid;
        $data = array(
            "number"=>$num,
        );
        $res = DB::table("order_goods")->where("id",$id)->update($data);
        if($res){
            echo 1;die;
        }else{
            echo 2;die;
        }
    }
    //订单删除
    public function todo_delorder(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $order_id = $request->order_id;
        $res = DB::table("order_goods")->where("order_id",$order_id)->get();
        if (sizeof($res)==0) {
            $res_2 = DB::table("order")->where("order_id",$order_id)->delete();
            if($res_2){
               echo json_encode(array(
                    "code"=>"1",
                    "msg"=>"删除成功"
                ));die; 
            }else{
                echo json_encode(array(
                    "code"=>"2",
                    "msg"=>"数据库执行有误"
                ));die;
            }
        }else{
            $res_3 = DB::table("order_goods")->where("order_id",$order_id)->delete();
            if ($res_3) {
                $res_4 = DB::table("order")->where("order_id",$order_id)->delete();
                if($res_4){
                   echo json_encode(array(
                        "code"=>"1",
                        "msg"=>"删除成功"
                    ));die; 
                }else{
                    echo json_encode(array(
                        "code"=>"2",
                        "msg"=>"数据库执行有误"
                    ));die;
                }
            }else{
                echo json_encode(array(
                    "code"=>"2",
                    "msg"=>"数据库执行有误"
                ));die;
            }
            
        }
    }
     //批量删除
     public function todo_delorders(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        // print_r($request->all());
        $idarr = $request->id;
        $res = DB::table("order_goods")->whereIn("order_id",$idarr)->get();
        if (sizeof($res)==0) {
            $res_2 = DB::table("order")->whereIn("order_id",$idarr)->delete();
            if($res_2){
               echo json_encode(array(
                    "code"=>"1",
                    "msg"=>"删除成功"
                ));die; 
            }else{
                echo json_encode(array(
                    "code"=>"2",
                    "msg"=>"数据库执行有误"
                ));die;
            }
        }else{
            $res_3 = DB::table("order_goods")->whereIn("order_id",$idarr)->delete();
            if ($res_3) {
                $res_4 = DB::table("order")->whereIn("order_id",$idarr)->delete();
                if($res_4){
                   echo json_encode(array(
                        "code"=>"1",
                        "msg"=>"删除成功"
                    ));die; 
                }else{
                    echo json_encode(array(
                        "code"=>"2",
                        "msg"=>"数据库执行有误"
                    ));die;
                }
            }else{
                echo json_encode(array(
                    "code"=>"2",
                    "msg"=>"数据库执行有误"
                ));die;
            }
        }
    }
    //订单商品删除
    public function todo_delordergood(Request $request){
        // $value = DB::table("good")->where("good_id",1)->first();
        //     if($value->iscommand==1){
        //         echo '<span>hot</span>';
        //     }else if($value->isnew==1){
        //         echo '<span>new</span>';
        //     }
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $res = DB::table("order_goods")->where("id",$request->id)->delete();
        if ($res) {
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"删除成功"
            ));die; 
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据库执行有误"
            ));die;
        }
    }
}